package service.calculator.sss;

import java.math.BigDecimal;
import java.util.List;

import javax.inject.Inject;

import models.Payment;
import models.SssContribution;
import service.PayrollCalculatorFacade;

public class DefaultSssContributionMapper implements SssContributionMapper {

    @Inject
    private static PayrollCalculatorFacade payrollCalculatorFacade;

    public SssContribution retrieveSssContribution(Payment payment) {
        SssContribution sssContribution = null;

        BigDecimal basicPayment = payrollCalculatorFacade.calculatePayment(
                payment.payroll.payrollPeriod.client.electronicId,
                payment.payroll.employee.rate, payment);

        List<SssContribution> contributions = SssContribution.findAll();
        for (SssContribution contribution : contributions) {

            int fromRangeComparison = contribution.salaryRangeFrom.compareTo(basicPayment);
            int toRangeComparison = contribution.salaryRangeTo.compareTo(basicPayment);
            boolean gtOrEqToFromRange = fromRangeComparison == 0 || fromRangeComparison == 1;
            boolean ltOrEqToToRange = toRangeComparison == 0 || toRangeComparison == -1;
            if (gtOrEqToFromRange && ltOrEqToToRange) {
                sssContribution = contribution;
                break;
            }
        }

        return sssContribution;
    }

}
